package com.banksoft.admin.controller;

import com.banksoft.admin.bean.MemberType;
import com.banksoft.admin.bean.OrderStatus;
import com.banksoft.admin.dao.OrderDao;
import com.banksoft.admin.dao.UserDao;
import com.banksoft.admin.entity.*;
import com.banksoft.admin.service.*;
import com.banksoft.common.BaseController;
import com.banksoft.common.ConfigHelper;
import com.banksoft.common.PageInfo;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringNumber;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vanjoger on 2017/2/10.
 */
@Controller
@RequestMapping("/orderFx")
public class OrderFxController extends BaseController{

    @Autowired
    OrderService orderService;
    @Autowired
    OrderFxService orderFxService;
    @Autowired
    BaseService baseService;
    @Autowired
    UserAddressService userAddressService;
    @Autowired
    TxApplyService txApplyService;

    @RequestMapping("/view")
    public String view(){
        return "yjjy";
    }

    @RequestMapping("/queryData")
    @ResponseBody
    public JSONObject queryData(String startDate,String endDate,String province,String city,String area){
        try{
            if(StringUtil.isNotNull(startDate))
                startDate += " 00:00:00";
            if(StringUtil.isNotNull(endDate))
                endDate += " 23:59:59";

            Map map = new HashMap();
            if(StringUtil.isNotNull(startDate))
                map.put("startDate", DateUtil.getLongFormStrDate(startDate));
            if(StringUtil.isNotNull(endDate))
                map.put("endDate",DateUtil.getLongFormStrDate(endDate));
            map.put("province", province);
            map.put("city", city);
            map.put("area", area);
            map.put("status", new String[]{OrderStatus.Pay.name(),OrderStatus.Send.name(),OrderStatus.Suc.name()});

            List<Order> orderList = this.orderService.count(map);
            String gwTotal = "0";
            String gwFxTotal = "0";
            String gwJy = "0";
            String sjTotal = "0";
            String sjFxTotal = "0";
            String sjJy = "0";
            if(StringUtil.isNotEmpty(orderList)){
                for(Order order:orderList){
                    List<OrderFx> fxlist = this.orderFxService.listByOrderId(order.getId());
                    if("0".equals(order.getType())){//购物订单
                        gwTotal = StringNumber.add(gwTotal,order.getTotalMoney());
                        if(StringUtil.isNotEmpty(fxlist)){
                            for(OrderFx dto:fxlist){
                                gwFxTotal = StringNumber.add(gwFxTotal,dto.getAmount());
                            }
                        }
                    }else if("1".equals(order.getType())){//升级订单
                        sjTotal = StringNumber.add(sjTotal,order.getTotalMoney());
                        if(StringUtil.isNotEmpty(fxlist)){
                            for(OrderFx dto:fxlist){
                                sjFxTotal = StringNumber.add(sjFxTotal,dto.getAmount());
                            }
                        }
                    }
                }
            }
            gwJy = StringNumber.sub(gwTotal,gwFxTotal);
            sjJy = StringNumber.sub(sjTotal,sjFxTotal);
           /* //提现统计
            map.put("status","1");
            List<TxApply> txApplyList = this.txApplyService.list(map);
            String txTotal = "0";
            String txDzTotal = "0";
            if(StringUtil.isNotEmpty(txApplyList)){
                for(TxApply apply:txApplyList){
                    txTotal = StringNumber.add(txTotal,apply.getAmount());
                    txDzTotal = StringNumber.add(txDzTotal,apply.getAmount());
                }
            }*/


            Map result = new HashMap();
            result.put("gwTotal",gwTotal);
            result.put("gwFxTotal",gwFxTotal);
            result.put("gwJy",gwJy);
            result.put("sjTotal",sjTotal);
            result.put("sjFxTotal",sjFxTotal);
            result.put("sjJy",sjJy);
           /* result.put("txTotal",txTotal);
            result.put("txDzTotal",txDzTotal);*/

            return this.FmtResult(true,null,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,null,null);
    }

    @RequestMapping("/list")
    public String list(String index, String startDate, String endDate, String province, String city, String area, Model model){
        model.addAttribute("index",index);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        model.addAttribute("province",province);
        model.addAttribute("city",city);
        model.addAttribute("area",area);
        return "yjjy_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String index, String startDate, String endDate, String province, String city, String area, PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        map.put("status",new String[]{OrderStatus.Pay.name(),OrderStatus.Send.name(),OrderStatus.Suc.name()});
        map.put("type",index);
        map.put("province",province);
        map.put("city",city);
        map.put("area",area);
        if(StringUtil.isNotNull(startDate)){
            startDate += " 00:00:00";
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        }
        if(StringUtil.isNotNull(endDate)) {
            endDate += " 23:59:59";
            map.put("endDate", DateUtil.getLongFormStrDate(endDate));
        }

        pageInfo.setParams(map);
        pageInfo = this.page(OrderDao.class,pageInfo);

        List<Order> list = pageInfo.getResult();
        if(StringUtil.isNotEmpty(list)){
            for(Order order:list){
                List<OrderFx> fxlist = this.orderFxService.listByOrderId(order.getId());
                String gwFxTotal = "0";
                if(StringUtil.isNotEmpty(fxlist)){
                    for(OrderFx dto:fxlist){
                        gwFxTotal = StringNumber.add(gwFxTotal,dto.getAmount());
                    }
                }
                order.setRemark(gwFxTotal);
            }
        }
        return this.getPageResult(pageInfo);
    }

    @RequestMapping("/loadDetails")
    @ResponseBody
    public JSONObject loadDetails(String id){
        List<OrderFx> list = this.orderFxService.listByOrderId(id);
        if(StringUtil.isNotEmpty(list)){
            for(OrderFx dto:list){
                User user = this.baseService.findById(UserDao.class,dto.getUserId());
                dto.setNickName(user.getNickName());
                if(!StringUtil.isNotNull(user.getCardName())){
                    List<UserAddress> userAddressList = userAddressService.listByUserId(user.getId());
                    if(StringUtil.isNotEmpty(userAddressList)){
                        UserAddress address = userAddressList.get(0);
                        dto.setUserName(address.getUserName());
                    }
                }else{
                    dto.setUserName(user.getCardName());
                }
                dto.setUserType(user.getType());
                if(MemberType.General.name().equals(user.getType())&&"1".equals(user.getIsPartner())){
                    dto.setUserType(MemberType.Partner.name());
                }
            }
        }
        return this.FmtResult(true,null,list);
    }



    /*@RequestMapping("/listTx")
    public String listTx(String startDate, String endDate, String province, String city, String area, Model model){
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        model.addAttribute("province",province);
        model.addAttribute("city",city);
        model.addAttribute("area",area);
        return "yjjy_tx_list";
    }

    @RequestMapping("/listTxData")
    @ResponseBody
    public Object listTxData(String startDate, String endDate, String province, String city, String area, PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        map.put("status","1");
        map.put("province",province);
        map.put("city",city);
        map.put("area",area);
        if(StringUtil.isNotNull(startDate)){
            startDate += " 00:00:00";
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        }
        if(StringUtil.isNotNull(endDate)) {
            endDate += " 23:59:59";
            map.put("endDate", DateUtil.getLongFormStrDate(endDate));
        }

        pageInfo.setParams(map);
        List<TxApply> list = this.txApplyService.searchByPage1(map);
        Integer count = this.txApplyService.count4Page1(map);


        pageInfo.setResult(list);
        pageInfo.setTotal(count);
        return this.getPageResult(pageInfo);
    }*/
}
